<?php 
include "header.inc";

$list = DataBase::select("subject",null,array("selection_campaign_id"=>$_SESSION["campaign_id"]));
$subjects = array();
if ($list <> false)
	foreach ($list as $s)
		$subjects[$s["subject_id"]] = $s;
		
$high_schools = DataBase::select("high_school",null,null,"high_school_name");

function get_ngo_name($id) {
	global $ngos;
	foreach ($ngos as $ngo)
		if ($ngo["ngo_id"] == $id)
			return $ngo["ngo_name"];
	return "?"; 
}
function get_high_school_name($id) {
	global $high_schools;
	foreach ($high_schools as $hs)
		if ($hs["high_school_id"] == $id)
			return $hs["high_school_name"];
	return "?"; 
}

$program_rules = array();
$rules_subjects = array();
$rules_by_type = array();
foreach ($programs as $p) {
	$rules = DataBase::select("exam_eligibility_rule",null,array("selection_campaign_id"=>$_SESSION["campaign_id"],"program_id"=>$p["program_id"]),"type");
	$program_rules[$p["program_id"]] = $rules;
	foreach ($rules as $rule) {
		$rule_id = $rule["exam_eligibility_rule_id"];
		$rule_subjects = DataBase::select("exam_eligibility_rule_subjects",null,array("exam_eligibility_rule_id"=>$rule_id),"subject_id");
		$rules_subjects[$rule_id] = $rule_subjects;

		if (!isset($rules_by_type[$rule["type"]])) $rules_by_type[$rule["type"]] = array();
		$subjects_str = "";
		foreach ($rule_subjects as $s) {
			if ($subjects_str<>"") $subjects_str .= " + ";
			$subjects_str .= $subjects[$s["subject_id"]]["subject_name"];
		}
		if (!isset($rules_by_type[$rule["type"]][$subjects_str])) $rules_by_type[$rule["type"]][$subjects_str] = array();
		if (!isset($rules_by_type[$rule["type"]][$subjects_str][$p["program_id"]])) $rules_by_type[$rule["type"]][$subjects_str][$p["program_id"]] = array();
		array_push($rules_by_type[$rule["type"]][$subjects_str][$p["program_id"]], $rule); 
	}
}
?>
<a href='general.php'>Go to the general statistics</a><br/>
<a href='../exams/eligibility/define_rules.php'>Go to the screen to define eligibility rules</a><br/>
<br/>
<table class='stats'>
<tr><th colspan=<?php echo 2+count($programs)?>>Rules</th></tr>
<tr><th>Rule Type</th><th>Subject(s)</th>
<?php foreach ($programs as $p) echo "<th>".$p["program_name"]."</th>"?>
</tr>
<?php
foreach ($rules_by_type as $type=>$rules_by_subjects) {
	foreach ($rules_by_subjects as $subjects_str=>$rules_by_program) {
		echo "<tr>";
		echo "<td nowrap='nowrap'>".($type == 0 ? "Minimum" : "Threshold")."</td>";
		echo "<td nowrap='nowrap'>".$subjects_str."</td>";
		foreach ($programs as $p) {
			if (!isset($rules_by_program[$p["program_id"]])) {
				echo "<td></td>";
				continue;
			}
			echo "<td align='right'>";
			$general = 0;
			$exceptions = "";
			foreach ($rules_by_program[$p["program_id"]] as $rule) {
				if ($rule["apply_to_ngo_id"]==null && $rule["apply_to_high_school_id"]==null)
					$general = $rule["mark"];
				else {
					if ($rule["apply_to_ngo_id"]<>null) {
						if ($rule["apply_to_high_school_id"]<>null) {
							$exceptions .= "<br/> For High School ".get_high_school_name($rule["apply_to_high_school_id"])." and NGO ".get_ngo_name($rule["apply_to_ngo_id"]);
						} else {
							$exceptions .= "<br/> For NGO ".get_ngo_name($rule["apply_to_ngo_id"]);
						}
					} else {
							$exceptions .= "<br/> For High School ".get_high_school_name($rule["apply_to_high_school_id"]);
					}
					$exceptions .= " = ".$rule["mark"];
				}
			}
			echo $general.$exceptions;
			echo "</td>";
		}
		echo "</tr>";
	}
} 
?>
</table>
<br/>
<form method="post">
View statistics for subject <select name='subject_id'>
<?php
foreach ($subjects as $sid=>$s) {
	echo "<option value='".$sid."'";
	if (isset($_POST["subject_id"]) && $_POST["subject_id"]==$sid) echo " selected='selected'";
	echo ">".$s["subject_name"]."</option>";
} 
?>
</select> having mark <select name='operator'>
<option value='=' <?php if (isset($_POST["operator"]) && $_POST["operator"]=="=") echo "selected='selected'"?>>=</option>
<option value='&lt;' <?php if (isset($_POST["operator"]) && $_POST["operator"]=="<") echo "selected='selected'"?>>&lt;</option>
<option value='&gt;' <?php if (isset($_POST["operator"]) && $_POST["operator"]==">") echo "selected='selected'"?>>&gt;</option>
</select> <input type='text' size=5 maxlength=5 name='mark' value='<?php if (isset($_POST["mark"])) echo $_POST["mark"]?>'/>
<br/>
<button type='submit'>Refresh</button>
</form>
<?php if (isset($_POST["subject_id"]) && isset($_POST["operator"]) && isset($_POST["mark"])) {
$subject_id = $_POST["subject_id"];
$op = $_POST["operator"];
$mark = $_POST["mark"];
if (!is_numeric($mark)) $mark = 0;
table(
	array("LEFT JOIN applicant_score_subject_exam AS score ON (score.applicant_id=applicant.applicant_id AND score.subject_id=".$subject_id.")"),
	array("score.score_subject_exam".$op.$mark)
);
}?>
<?php 
include "footer.inc";
?>
